<?php

namespace app\common\model\xiluhouse;

use think\Model;
use traits\model\SoftDelete;

class Rental extends Base
{

    use SoftDelete;

    // 表名
    protected $name = 'xiluhouse_rental';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        // 'images_arr',
        // 'house_tag_ids_text_arr',
        // 'building_descript_arr',
        'image_text',
        'is_public_text',
        'state_text',
        'viewtime_text',
        'status_text',
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }
    // 处理建筑信息
    public function getBuildingDescriptArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['building_descript']) ? $data['building_descript'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }

    // 处理多图的情况
    public function getImagesArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $images_arr = $value ? explode(',', $value) : [];
        foreach ($images_arr as $now_image_key => $now_image_value) {
            $images_arr[$now_image_key] = cdnurl($now_image_value,true);
        }
        return $images_arr;
    }
    public function getHouseTagIdsTextArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['house_tag_ids']) ? $data['house_tag_ids'] : '');
        $tag_name_arr = [];
        if ($value) {
            $tag_name_arr = HouseTag::where('status', 'normal')->where('id', 'in', $value)->orderRaw('weigh desc,id desc')->column('name');
        }
        return $tag_name_arr;
    }

    public function getIsPublicList()
    {
        return ['0' => __('Is_public 0'), '1' => __('Is_public 1')];
    }

    public function getStateList()
    {
        return ['1' => __('State 1'), '2' => __('State 2')];
    }

    public function getStatusList()
    {
        return ['normal' => __('上架中'), 'hidden' => __('已下架')];
    }
    public function getIsPublicTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_public']) ? $data['is_public'] : '');
        $list = $this->getIsPublicList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getViewtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['viewtime']) ? $data['viewtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setViewtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function village()
    {
        return $this->belongsTo('Village', 'village_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    //前端需要innerjoin
    public function village2()
    {
        return $this->belongsTo('Village', 'village_id', 'id', [], 'INNER')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function housetype()
    {
        return $this->belongsTo('HouseType', 'house_type_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 获取房源标签-建筑信息值
     * @param int $type 1=标签,2=房源信息
     * @return bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function rentalTags($type=1){
        $lists = RentalTag::field(['id','rental_id','tag_type_id','tag_id'])->with(['tagType'=>function($query) use($type){
            $query->withField(['id','name'])->where('type',$type);
        },'tag'=>function($query){
            $query->withField(['id','name']);
        }])->where('rental_id',$this->id)->select();
        return $lists;
    }

    /**
     * 经纪人获取代理房源
     * @param $user_id
     */
    public static function getAgentCount($user_id){
        $village_ids = VillageBroker::where('broker_user_id',$user_id)->column('village_id');
        if(!$village_ids){
            return 0;
        }
        return self::whereIn('village_id',$village_ids)->where('user_id','neq',$user_id)->where('is_public',1)->count('id');
    }

}
